package cn.kenny.resource.service.impl;

import cn.kenny.resource.entity.PageResult;
import cn.kenny.resource.entity.QueryPageBean;
import cn.kenny.resource.mapper.DocMapper;
import cn.kenny.resource.mapper.UserMapper;
import cn.kenny.resource.pojo.Doc;
import cn.kenny.resource.pojo.User;
import cn.kenny.resource.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DocMapper docMapper;

    public List<User> findUserAll() {
        return userMapper.findAll();
    }

    public PageResult userPageQuery(QueryPageBean queryPageBean){
        //接收参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //完成分页查询，基于mybatis框架提供的分页助手晚餐
        PageHelper.startPage(currentPage,pageSize);//不需要返回值，基于线程绑定，绑定当前线程
        Page<User> page;
        if(queryString == null || queryString.length() <= 0){
            page = userMapper.findAll();
        }else{
            page = userMapper.selectByCondition(queryString);
        }
        long total = page.getTotal();
        List<User> rows = page.getResult();
        return new PageResult(total,rows);
    }

//    public List<Doc> findDocAll(){ return docMapper.findAll(); }

    public PageResult docPageQuery(QueryPageBean queryPageBean){
        //接收参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //完成分页查询，基于mybatis框架提供的分页助手晚餐
        PageHelper.startPage(currentPage,pageSize);//不需要返回值，基于线程绑定，绑定当前线程
        Page<Doc> page;
        if(queryString == null || queryString.length() <= 0){
            page = docMapper.findAll();
        }else{
            page = docMapper.selectByCondition(queryString);
        }
        long total = page.getTotal();
        List<Doc> rows = page.getResult();
        return new PageResult(total,rows);
    }

    public void addUser(User user){
        long count = userMapper.findCountByTel(user.getTelphone());
        if(count > 0){
            throw new RuntimeException("此手机号已存在");
        }
        userMapper.add(user);
    }

    public void editUser(User user){ userMapper.edit(user); }

    public int deleteById(Integer id){
        return userMapper.deleteById(id);
    }


}
